﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jw.Web.Areas.Pacm.Models;
using System.Web.Http;
using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Mms.Common;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Pacm.Controllers
{
    public class JsbzjhController : BaseController
    {
        // GET: /Pacm/Jsbzjh/
        public ActionResult Index()
        {
            //年份
            List<ComItem> comYearItems = new List<ComItem>();
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(-1).Year.ToString(), value = DateTime.Now.AddYears(-1).Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.Year.ToString(), value = DateTime.Now.Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(1).Year.ToString(), value = DateTime.Now.AddYears(1).Year.ToString() });


            //月份
            List<ComItem> comMonthItems = new List<ComItem>();
            for (int i = 0; i < 12; i++)
            {
                ComItem obj = new ComItem();
                obj.text = obj.value = (i + 1).ToString("00");
                comMonthItems.Add(obj);
            }

            var model = new
            {
                dataSource = new
                {
                    yearsItems = comYearItems,
                    monthItems = comMonthItems
                },
                form = new
                {
                    Jsbzjh_Year = DateTime.Now.Date.Year,
                    Jsbzjh_Month = "",
                    UpdateDate = ""
                }

            };
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var userName = FormsAuth.GetUserData().UserName;
            var data = new JsbzjhApiController().GetEditMaster(id);
            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                dataSource = new
                {
                },
                defaultForm = new p_Jsbzjh().Extend(new
                {
                    Jsbzjh_BillNo = id,
                    Jsbzjh_Year = DateTime.Now.Year.ToString(),
                    Jsbzjh_Month = DateTime.Now.Month.ToString("00"),
                    CreatePerson = userName,
                    CreateDate = DateTime.Now
                }),
                setting = new
                {
                    postFormKeys = new string[] { "Jsbzjh_BillNo" }
                }
            };

            return View(model);
        }
    }

    public class JsbzjhApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Jsbzjh_BillNo'>
                <select>
                   p_Jsbzjh.*
                </select>
                <from>
                    p_Jsbzjh
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Jsbzjh_BillNo'             cp='equal'     ></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new p_JsbzjhService().GetDynamicListWithPaging(pQuery);
            return list;
        }

        public string GetNewBillNo()
        {
            return new p_JsbzjhService().GetNewKey("Jsbzjh_BillNo", "dateplus");
        }

        public dynamic GetEditMaster(string id)
        {
            p_Jsbzjh obj = new p_JsbzjhService().GetModel(ParamQuery.Instance().AndWhere("Jsbzjh_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new p_JsbzjhService().ScrollKeys("Jsbzjh_BillNo", id.ToString())
            };
        }

        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Jsbzjh_BillNo' cp='equal'></field></where>
            </settings>", typeof(p_Jsbzjh).Name);

            var result = new p_JsbzjhService().Edit(formWrapper, null, data);
        }

        public void Delete(string id)
        {
            var result = new p_JsbzjhService().Delete(ParamDelete.Instance().AndWhere("Jsbzjh_BillNo", id));
            MmsHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Jsbzjh_BillNo={0}]，请重试或联系管理员！", id);
        }
    }

}
